package com.neptune.imapp.ui.compose.screen.frontPage

import android.util.Log
import androidx.compose.runtime.MutableState
import androidx.compose.runtime.mutableStateOf
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import androidx.navigation.NavController
import com.google.accompanist.pager.ExperimentalPagerApi
import com.google.accompanist.pager.PagerState
import com.neptune.imapp.repository.entity.BaseInfo
import com.neptune.imapp.repository.service.UserService
import com.neptune.imapp.repository.vo.UserVO
import com.neptune.imapp.ui.compose.navigation.RouterConfig
import com.neptune.imapp.ui.compose.screen.BasicViewModel
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.launch
import javax.inject.Inject
import kotlin.concurrent.timer

@HiltViewModel
class FrontPageViewModel @Inject constructor() : ViewModel() {
    @Inject
    lateinit var userService: UserService

    @Inject
    lateinit var basicViewModel: BasicViewModel
    private val tag = "首页ViewModel"
    val phoneVal = mutableStateOf("")
    val passwordVal = mutableStateOf("")
    val passwordVisibility = mutableStateOf(true)
    val isPhoneError = mutableStateOf(false)
    val captcha = mutableStateOf("")
    val repeatPassword = mutableStateOf("")
    val baseInfo = BaseInfo()
    val phoneError = mutableStateOf(false)
    val passwordError = mutableStateOf(false)
    val errorMessage = mutableStateOf("")

    fun passwordInputClick() {
        passwordVisibility.value = !passwordVisibility.value
    }

    fun clearString(str: MutableState<String>) {
        str.value = ""
    }

    fun login(navController: NavController) {
        viewModelScope.launch {
            val result =
                userService.loginAsync(username = phoneVal.value, password = passwordVal.value)
            if (result.code == 0) {
                errorMessage.value = result.msg
                if (errorMessage.value == "用户密码错误！") {
                    passwordError.value = true
                } else {
                    phoneError.value = true
                }
            } else {
                basicViewModel.token = result.data
                basicViewModel.telephone = phoneVal.value
                navController.navigate(RouterConfig.ROUTE_Main)
            }
        }
    }

    fun postBasicInfo(navController: NavController) {
        viewModelScope.launch {
            val userVO = UserVO(
                id = baseInfo.id,
                username = baseInfo.username.value,
                sex = baseInfo.sex.value,
                birthDay = baseInfo.birthDay.value,
                email = baseInfo.email.value
            )
            val result = userService.postBasicInfo(userVO)
            when (result.code) {
                1 -> {
                    navController.navigate(RouterConfig.ROUTE_Login)
                    phoneVal.value = baseInfo.telephone.value
                    passwordVal.value = baseInfo.password.value
                }
                0 -> {
                    Log.i(tag, "注册失败")
                }
            }
        }
    }

    @OptIn(ExperimentalPagerApi::class)
    fun register(pagerState: PagerState) {
        viewModelScope.launch {
            val result = userService.register(
                telephone = baseInfo.telephone.value,
                password = baseInfo.password.value
            )
            Log.i(tag, "register: $result")
            when (result.code) {
                1 -> {
                    baseInfo.id = result.data.toLong()
                    pagerState.scrollToPage(1)
                }
                0 -> {
                    Log.i(tag, "注册失败")
                }
            }
        }
    }

    val telephoneCheck = mutableStateOf(false)
    fun checkTelephone(time: MutableState<String>) {
        viewModelScope.launch {
            val result =
                userService.checkTelephone(baseInfo.telephone.value)
            if (result.code == 0) {
                telephoneCheck.value = true
            } else {
                time.value = "60"
                timer(period = 1000) {
                    time.value = "${time.value.toInt() - 1}"
                    if (time.value == "0") {
                        time.value = "Get"
                        cancel()
                    }
                }
            }

        }
    }

}